package Chapter8Test;
/*12-29
1.声明ball类，描述球的坐标、直径、颜色、移动方向、动力和速度等属性，提供draw(Graphics)方法，以实例的坐标、直径、颜色等属性画球
2.画出若干2D风格的彩色弹弹球，每个球获得大小不等的初始动力和速度，运动方向也各不相同，运行过程中速度逐渐减小，双击它，使它再获动力
3.设计球属性设置对话框，添加若干球，设置每个球的属性
4.将每个球的属性写入对象文件保存，再次读取，演示动画
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class MyFrame extends JFrame implements ActionListener {
    private MyDialog jdialog;
    private BallCanvas ballCanvas;
    private JPanel cmdpanel;
    private JButton[] button;
    private String[] str = {"设置", "开始", "暂停"};


    public MyFrame() {
        super("彩色弹弹球");
        this.setBounds(400, 150, 1000, 600);
        cmdpanel = new JPanel(); //命令面板，流布局，居中
        cmdpanel.setSize(1000, 100);
        button = new JButton[str.length];
        for (int i = 0; i < str.length; i++) {
            button[i] = new JButton(str[i]);
            button[i].addActionListener(this);
            cmdpanel.add(button[i]);
        }
        cmdpanel.setForeground(Color.gray);
        this.add(cmdpanel, BorderLayout.SOUTH);
        ballCanvas = new BallCanvas();
        ballCanvas.setPreferredSize(new Dimension(1000, 500));
        this.add(ballCanvas, BorderLayout.CENTER);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //this.jdialog=new MyDialog();
    }

    class Ball {
        int X, Y; //球的坐标
        Color ballColor;  //球的颜色
        final int ballSize = 16; //球的大小
        int  speedX, speedY;  //横向速度与纵向速度

        public Ball(int X, int Y, Color ballColor, int speedX, int speedY) {
            this.X = Y;
            this.Y = Y;
            this.ballColor = ballColor;
            this.speedX = speedX;
            this.speedY = speedY;
        }

    }

    class BallCanvas extends Canvas implements ActionListener, MouseListener {
        Color[] colors = {Color.red, Color.orange, Color.yellow, Color.GREEN, Color.blue, Color.magenta, Color.black};
        Ball[] balls = new Ball[colors.length];   //初始时球的数量

        //在Canvas上作图
        public void paint(Graphics p) {
            for (int i = 0; i < balls.length; i++) {      //设置球的属性
                balls[i] = new Ball(new Random().nextInt(1000) + 400, new Random().nextInt(400) + 150, colors[new Random().nextInt(7)],
                        new Random().nextInt(10) * 10, new Random().nextInt(10) * 10);
                p.setColor(balls[i].ballColor);//设置球的颜色
                p.fillOval(balls[i].X, balls[i].Y, balls[i].ballSize, balls[i].ballSize);    //填充球

            }
        }

        public BallCanvas() {   //画布的构造方法，背景颜色，以及注册鼠标监听
            this.setBackground(Color.white);
            this.addMouseListener(this);
            ActionListener task = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    for (int i = 0; i < balls.length; i++) {
                        balls[i].X += balls[i].speedX;
                        balls[i].Y += balls[i].speedY;
                        balls[i].speedX -= 5;
                        balls[i].speedY -= 5;
                        //重绘画面
                        ballCanvas.repaint();
                    }
                }
            };
            Timer timer=new Timer(100,task);
            timer.start();
        }


        @Override
        public void actionPerformed(ActionEvent event) {
            for (int i = 0; i < balls.length; i++) {
                if (balls[i].X <= 400 || balls[i].Y >= 1400)
                    balls[i].speedX = -balls[i].speedX;
                if (balls[i].Y <= 150 || balls[i].Y >= 650)
                    balls[i].speedY = -balls[i].speedY;
            }
            ballCanvas.repaint();
        }

        @Override
        public void mouseClicked(MouseEvent event) {
            for (int i = 0; i < balls.length; i++) {
                if (event.getX() == balls[i].X && event.getY() == balls[i].Y) {
                    balls[i].speedX *= 2;  //单击小球，速度变双倍
                    balls[i].speedY *= 2;
                }

            }

        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }

    class MyDialog extends JDialog implements ActionListener {
        int X, Y; //球的坐标
        Color ballColor;  //球的颜色
        int ballSize;
        double speedX, speedY;  //横向速度与纵向速度

        JTextField[] property;
        JPanel nPanel, sPanel;
        JComboBox<String> color;
        String[] buttonstr = {"确认", "取消"};
        String[] labelstr = {"X坐标", "Y坐标", "X方向的速度", "Y方向上的速度", "球的大小", "球的颜色"};
        String[] colors = {"红色", "橙色", "黄色", "绿色", "蓝色", "紫色", "黑色"};

        public MyDialog() {
            super(MyFrame.this, "设置", true);
            this.setBounds(500, 200, 800, 120);
            nPanel = new JPanel();
            nPanel.setSize(600, 100);
            property = new JTextField[labelstr.length - 1];
            for (int i = 0; i < labelstr.length; i++) {
                nPanel.add(new JLabel(labelstr[i]));   //添加标签
                if (i != labelstr.length - 1) {
                    property[i] = new JTextField(5);
                    nPanel.add(property[i]);
                }
            }
            color = new JComboBox<>(colors);
            nPanel.add(color);        //添加组合框
            this.add(nPanel, BorderLayout.CENTER);
            sPanel = new JPanel();
            for (int i = 0; i < buttonstr.length; i++) {
                sPanel.add(new JButton(buttonstr[i]));   //添加两个按钮
            }
            this.add(sPanel, BorderLayout.SOUTH);
            this.setDefaultCloseOperation(HIDE_ON_CLOSE);
            this.setBackground(Color.gray);
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            try {
                X = Integer.parseInt(property[0].getText());
                Y = Integer.parseInt(property[1].getText());
                if (X < 400 || X > 1400 || Y < 150 || Y > 750)
                    JOptionPane.showMessageDialog(this, "输入坐标越界，请重新输入！");
                speedX = Integer.parseInt(property[2].getText());
                speedY = Integer.parseInt(property[3].getText());
                ballSize = Integer.parseInt(property[4].getText());
                if (ballSize >= 600)
                    JOptionPane.showMessageDialog(this, "球的体积过大，界面无法存入，请减小球的直径！");
                switch (color.getSelectedIndex()) {
                    case 0:
                        ballColor = Color.RED;
                        break;
                    case 1:
                        ballColor = Color.ORANGE;
                        break;
                    case 2:
                        ballColor = Color.YELLOW;
                        break;
                    case 3:
                        ballColor = Color.GREEN;
                        break;
                    case 4:
                        ballColor = Color.BLUE;
                        break;
                    case 5:
                        ballColor = Color.MAGENTA;
                        break;
                    case 6:
                        ballColor = Color.BLACK;
                        break;
                }
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "输入错误，请重新输入！");
            }
            switch (event.getActionCommand()) {
                case "确认":
                    this.setVisible(false);
                    break;
                case "取消":
                    this.setVisible(false);
                    break;
            }
        }

    }


    @Override
    public void actionPerformed(ActionEvent event) {
        switch (event.getActionCommand()) {
            case "设置":
                jdialog = new MyDialog();
                jdialog.setVisible(true);

                break;
            case "开始":

                this.button[1].setEnabled(false);
                this.button[2].setEnabled(true);
                break;
            case "暂停":
                this.button[1].setEnabled(true);
                this.button[2].setEnabled(false);
                break;
        }
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}
